<?php
/**
* 更新批发客户等级
 */

namespace application\timer;


use application\depend\Applog;
use application\depend\CurlRequest;
use application\depend\Dbiov;
use application\depend\PhpRedis;

class UpdateClientLevel
{

    public function update_client(){

        $flag = PhpRedis::get('update_client_level');

        if($flag && $flag == 'yes'){
            
            PhpRedis::set('update_client_level',NULL);
            $sql = "SELECT 
                        id,name
                    FROM w_clients
            ";
            $pdo = Dbiov::getInstance();
            $pre = $pdo->prepareSql($sql);
            try{
                if($pre->execute()){
                    $res = $pre->fetchAll();  //获取批发客户等级
                    // if($res){

                    // }
                }

            }catch(\Exception $e){

            }


            $sql_level = "SELECT * FROM wlevel";
            $pre_lvl = $pdo->prepareSql($sql_level);

            try{
                if($pre_lvl->execute()){
                    $res_lvl = $pre_lvl->fetchAll();  //获取等级判断范围信息
                    // if($res){

                    // }
                }

            }catch(\Exception $e){

            }

            //客户信息不为空
            if($res){
                foreach($res as $k=>$v){
                    $sql_order = "SELECT order_number from `order` WHERE customer_name='" . $v['name']. "' and status=4";
                    $pre_order = $pdo->prepareSql($sql_order);
                    try{
                        if($pre_order->execute()){
                            $res_order = $pre_order->fetchAll();  //获取等级判断范围信息
                            if($res_order){
                                $total_sum=0;   //初始化 总金额
                                //查询对应客户的对应订单号总消费金额  包含折扣  （每一笔订单的金额总和）  统计order_info
                                foreach($res_order as $k1=>$v1){

                                    /**  订单总额   */
                                    $sql_total_price = "SELECT sum(a.total_price*a.discount_rate) as price from order_info as a 
                                                        INNER join `order` b ON a.order_number=b.order_number 
                                                        where b.status=4 and a.order_number='" . $v1['order_number'] . "' and a.customer_name='" . $v['name'] . "'";

                                    $pre_total_price = $pdo->prepareSql($sql_total_price);
                                    try{
                                        if($pre_total_price->execute()){
                                            $res_total_price = $pre_total_price->fetchAll();  //获取等级判断范围信息
                                            
                                            $total_sum += $res_total_price[0]['price'];  //累积所有订单的折后价
                                            
                                        }
                        
                                    }catch(\Exception $e){
                        
                                    }



                                    /**  运费总额   */
                                    $sql_sum_feright = "SELECT sum(feright) as feright_sum 
                                                        FROM `order` 
                                                        WHERE customer_name='" . $v['name'] . "'";
                                        
                                    $pre_sum_feright = $pdo->prepareSql($sql_sum_feright);
                                    try{
                                        if($pre_sum_feright->execute()){
                                            $res_sum_feright = $pre_sum_feright->fetchAll();  //获取等级判断范围信息
                                            
                                            
                                            //总金额+运费  =  总消费金额 （用来统计客户等级的数据）
                                            $levelCheck = $total_sum + $res_sum_feright[0]['feright_sum'];
                                        }
                        
                                    }catch(\Exception $e){
                        
                                    }
                                    
                                }//foreach 2
                            }//if

                            //批发客户等级评定
                 
                            if($levelCheck && $res_order){
                                if($levelCheck>$res_lvl[0]['level1'] && $levelCheck<=$res_lvl[0]['level1T']){
                                    /*一星客户*/
                                    $res[$k]['level'] = 1;
            
            
                                }elseif ($levelCheck>$res_lvl[0]['level2'] && $levelCheck<=$res_lvl[0]['level2T']) {
                                    /*二星客户*/
                                    $res[$k]['level'] = 2;
            
            
                                }elseif ($levelCheck>$res_lvl[0]['level3'] && $levelCheck<=$res_lvl[0]['level3T']) {
                                    /*三星客户*/
                                    $res[$k]['level'] = 3;
            
            
                                }elseif ($levelCheck>$res_lvl[0]['level4'] && $levelCheck<=$res_lvl[0]['level4T']) {
                                    /*四星客户*/
                                    $res[$k]['level'] = 4;
            
            
                                }elseif ($levelCheck>$res_lvl[0]['level5']) {
                                    /*五星客户*/
                                    $res[$k]['level'] = 5;
            
                                }else{   //如果没有对应的客户名称， 是无法查询到金额+客户名， 也就是说， $levelCheck 不会有任何结果集。  就当做新客户
                                    $res[$k]['level'] = 0;
                                }
                            }//if
                            else{//如果没有对应的客户名称， 是无法查询到金额+客户名， 也就是说， $levelCheck 不会有任何结果集。  就当做新客户
                                $res[$k]['level'] = 0;
                            }

                            //将更新的等级结果更新
                            // var_dump($res[$k]);
                            $client_level_new = $res[$k]['level'];
                            $client_id = $res[$k]['id'];
                            $sql_updatelvl = "UPDATE w_clients set level=$client_level_new WHERE id=$client_id";
                        
                            $pre_updatelvl = $pdo->prepareSql($sql_updatelvl);
                            try{
                                if(!$pre_updatelvl->execute()){
                                    $err = [
                                        'title'=>'客户'.$res[$k]['name'].'等级更新失败',
                                    ];
                                   Applog::errorLog($err);
                                    
                                }
                
                            }catch(\Exception $e){
                
                            }
                           

                        }//if excute

                        
        
                    }catch(\Exception $e){
        
                    }
                }//foreach 1
            }//if

            $pdo->closeDb();
        }//if yes
        else{
            // var_dump('没进来！');
        }
    }

}